class PersonController < ApplicationController

#Main method for retrieve data via SRU
  
def sru
  @name=nil
  require 'libxml'  
  require 'libxslt'
  dn ='dn:http://www.loc.gov/MARC21/slim'
  fac ='fac:http://www.indexdata.com/zebra/'
  
  #Helper method to get ZEBRA-facet for the result set
  #IMPORTANT: Zebra facects are not relieable at the moment
  #Parameters: XML-Document, Namespace, Indexfield
  def getFacet(doc, dn, index)
      facet_place=doc.find('//fac:record/fac:facet[@index="'+index+'"]/fac:term[@occur]',dn).to_a[0..4]
          f_place=[]
          facet_place.each {|node|
            if node.content.size>1
              content=node.content.capitalize
            else
              content=node.content
            end
            f_place << [content, "("+node['occur']+")"]
          }
          f_place
  end
   
 #Helper method to convert SRU-search parameter to SOLR-search parameter
def sru2solr(text)
  res=[]
  if !text.include?('=')
    res<< text
  elsif text.split('=').size==2
    if text.split('=')[1].include?('*')
    res << text.split('=')[0]+':'+text.split('=')[1]
else
res << text.split('=')[0]+':"'+text.split('=')[1]+'"'
end
  else
    res<<text.split('=')[0]+':'
    text.split('=')[1..-1].each {|v|
      if v.include?(" and ")
        if v.split('and')[0].include?('*')
        res << v.split('and')[0].strip
      else
        res << '"'+ v.split('and')[0].strip+'"'
end
        res << "AND"
        res<<v.split('and')[1]+':'
      else
        if v.include?('*')
          res << v
        else
          res << '"'+v+'"'
        end
      end
    }
  end
  res.join(' ').gsub(' and ',' AND ')
end
     
def getSolrFacet(doc, index)
    facet_place=doc.find('//lst/lst/lst[@name="'+index+'"]/child::node()').to_a
        f_place=[]
        facet_place.each {|node|
          node.each {|e|
            if node.content.to_i>0
              if node["name"].size>1
                            content=node["name"].capitalize
              else
                content=node["name"]
              end
          f_place << [content,"("+String(node.content)+")"]
          end 
          }
        }
        f_place
end
        
  
  @para=params
  if params["sru"]
  @par=params
  @name=params["sru"]["name"] if params["sru"]["name"]
    #@solr_name=[]
    #@name.split("=").join(":").split(" ").each {|s|
      #if s.include?(":")
      #@solr_name << s.split(":")[0]+":"
      #@solr_name <<'"'+s.split(":")[1]+'"'
    
#else
  #@solr_name<<s
  #end
#}
    #@solr_name=String(@name).gsub("=",":").gsub(" and ", " AND ")
    
  end
  @start=params["start"] if params["start"]
  url='http://lab.rism.info:9910/person?operation=searchRetrieve&version=1.1&query='+String(@name)+'&maximumRecords=20&startRecord='+String(@start)+'&sortKeys=author'
  xml_document =LibXML::XML::Document.file(url)
  
  #facet_url='http://lab.rism.info:9910/person?operation=searchRetrieve&version=1.1&query='+String(@name)+'&maximumRecords=1&recordSchema=zebra::facet::place:p,profession:p,gender:w'
#facet_document =LibXML::XML::Document.file(facet_url)
if params["sru"]
  @solr_facet_url='http://lab.rism.info:8080/solr/person/select/?q='+sru2solr(@name)+'&version=2.2&start=0&rows=0&indent=on&facet=true&facet.field=place&facet.field=country&facet.field=profession&facet.field=gender&facet.limit=6'
  solr_facet_document =LibXML::XML::Document.file(@solr_facet_url)
#@solr_facet_place=solr_facet_document.find('//lst/lst/lst[@name="place_m"]').to_a
  @facet_place=getSolrFacet(solr_facet_document,'place')
  @facet_profession=getSolrFacet(solr_facet_document,'profession')
  @facet_gender=getSolrFacet(solr_facet_document,'gender')
  @facet_country=getSolrFacet(solr_facet_document,'country')


end  

  #@facet=facet_document.find('//fac:record/fac:facet',fac).to_a
  


#@facet_place=getSolrFacet(solr_facet_document,'place_m')
#@facet_place=getFacet(facet_document,fac,'place')
#@facet_profession=getFacet(facet_document,fac,'profession')
#@facet_gender=getFacet(facet_document,fac,'gender')
  
  

  
  
  #@facet_place=facet_document.find('//fac:record/fac:facet[@index="place"]/fac:term[@occur>5]',fac).to_a
  #@facet_place=facet_document.find('//fac:record/fac:facet[@index="place"]/fac:term[@occur>5]',fac).to_a
  

  
  
  
  
  @count=xml_document.find('//zs:numberOfRecords/text()')[0]
  style='/home/dev/projects/rismtoolkit/app/assets/person.xsl'
  stylesheet_doc=LibXML::XML::Document.file(style)
  stylesheet=LibXSLT::XSLT::Stylesheet.new(stylesheet_doc)
  @res=stylesheet.apply(xml_document)  
  
end
end

def info
  @updated="Updated 2013-07-30"
end
